//
//  LZNewFeatureController.swift
//  Weibo
//
//  Created by lzing on 16/3/24.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class LZNewFeatureController: UICollectionViewController {
    
    let layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        ///  布局collectionView属性
        prepareCollectionView()
    }

    ///  布局collectionView属性
    private func prepareCollectionView() {
        // 设置layout属性
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        // Register cell classes
        self.collectionView!.registerClass(LZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
}

// MARK: - collectionViewDelegate&&Datasource
extension LZNewFeatureController {
    // 返回Item个数
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return LZNewFeatureCount
    }
    // 返回cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LZNewFeatureCell
        
        cell.index = indexPath.item
        
        return cell
    }
    // 滚动停止
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        // 计算当前页
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width)
        // 如果是最后一页就开启动画
        if page == LZNewFeatureCount - 1 {
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            // 取出对应的cell
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! LZNewFeatureCell
            // 开启按钮动画
            cell.startBtnAnimation()
        }
    }
}

class LZNewFeatureCell: UICollectionViewCell {
    //MARK: - attribute
    var index : Int = 0{
        didSet {
            // 根据下标设置图片
            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            // 隐藏按钮
            startBtn.hidden = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 准备ui
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - startBtnDidClick
    @objc private func startBtnDidClick() {
        AppDelegate.outSwitchRootViewController(LZMainController())
    }
    
    //MARK: - startBtnAnimation
    private func startBtnAnimation() {
        // 显示按钮
        startBtn.hidden = false
        // 开启动画
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.startBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    //MARK: - prepareUI
    private func prepareUI() {
        // 添加子控件
        addSubviews()
        // 添加约束
        addConstraint()
    }
    
    ///  添加子控件
    private func addSubviews() {
        addSubview(bkgImageView)
        addSubview(startBtn)
    }
    ///  添加约束
    private func addConstraint() {
        // 为背景图添加约束
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
        // 为开始按钮添加约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
    }
    
    // MARK: - lazy
    /// 背景图
    private lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    /// 开始按钮
    private lazy var startBtn : UIButton = {
        var button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "startBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
